package cn.darven.ui;


import javax.swing.*;
import java.awt.*;

/**
 * @author darven
 * @date 2025/11/3
 * @description 主界面
 */
public class MainFrame extends JFrame {
    private JLabel statusLabel;
    private JTextArea textArea;
    private DatabaseManager databaseManager;
    private TransformManager transformManager;
    private DownloadManager downloadManager;

    public MainFrame() {
        databaseManager = new DatabaseManager(this);
        transformManager = new TransformManager(this);
        downloadManager = new DownloadManager(this);
        initializeComponents();
    }

    private void initializeComponents() {
        setTitle("SQLForXML");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        add(createToolBar(), BorderLayout.NORTH);
        add(createMainPanel(), BorderLayout.CENTER);
        add(createStatusBar(), BorderLayout.SOUTH);
    }

    private JPanel createMainPanel() {
        JPanel mainPanel = new JPanel(new BorderLayout());
        textArea = new JTextArea();
        textArea.setEditable(false);
        JScrollPane jScrollPane = new JScrollPane(textArea);
        mainPanel.add(jScrollPane, BorderLayout.CENTER);
        return mainPanel;
    }

    private JPanel createStatusBar() {
        JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
        statusLabel = new JLabel("就绪");
        statusBar.add(statusLabel);
        statusBar.setBorder(BorderFactory.createLoweredBevelBorder());
        return statusBar;
    }

    private JToolBar createToolBar() {
        JToolBar toolBar = new JToolBar();

        JButton connectButton = new JButton("连接数据库");
        connectButton.addActionListener(e -> databaseManager.connectToDatabase());
        toolBar.add(connectButton);

        JButton queryButton = new JButton("执行查询");
        queryButton.addActionListener(e -> databaseManager.executeQuery(textArea));
        toolBar.add(queryButton);

        JButton transformButton = new JButton("XML转换HTML");
        transformButton.addActionListener(e -> transformManager.performTransform());
        toolBar.add(transformButton);

        JButton downloadButton = new JButton("下载");
        JPopupMenu downloadMenu = new JPopupMenu();

        JMenuItem downloadHtml = new JMenuItem("下载HTML文件");
        downloadHtml.addActionListener(e -> downloadManager.downloadHtml());
        JMenuItem downloadXML = new JMenuItem("下载XML文件");
        downloadXML.addActionListener(e -> downloadManager.downloadXML());
        downloadMenu.add(downloadHtml);
        downloadMenu.add(downloadXML);

        downloadButton.addActionListener(e -> {
            downloadMenu.show(downloadButton, 0, downloadButton.getY() + 30);
        });

        toolBar.add(downloadButton);

        toolBar.addSeparator();

        JButton refreshButton = new JButton("刷新");
        refreshButton.addActionListener(e -> textArea.setText(""));
        toolBar.add(refreshButton);

        return toolBar;
    }

    public void updateStatus(String status) {
        statusLabel.setText(status);
    }

    public DatabaseManager getDatabaseManager() {
        return databaseManager;
    }

    public TransformManager getTransformManager() {
        return transformManager;
    }
}